package com.biz.primus.app.erpbridge.controller;

import com.biz.primus.app.erpbridge.feign.MemberFeignClient;
import com.biz.primus.app.erpbridge.feign.MiscFeignClient;
import com.biz.primus.model.member.vo.MemberVo;
import com.biz.primus.model.member.vo.request.MemberRegisterRequestVo;
import com.biz.primus.model.member.vo.response.MemberRegisterResponseVo;
import com.biz.primus.model.misc.enums.SMSType;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author jun.liu
 * @usage
 * @reviewer
 * @since 2017年11月29日
 */
@RestController
@RequestMapping("bridge/member/")
public class MemberBridgeController extends BaseBridgeController
{

    @Autowired
    private MemberFeignClient memberFeignClient;

    @Autowired
    private MiscFeignClient miscFeignClient;

    @PostMapping("sendSMS")
    @ApiOperation(value = "给指定手机号发送短信", position = 1)
    public void sendSMS(@RequestParam("mobile") @ApiParam("手机号") String mobile,
                        @ApiParam("短信类型:\n" +
                                    "    REGISTER(\"用户注册\"),\n" +
                                    "    FORGOT_PASSWORD(\"忘记密码\"),\n" +
                                    "    REGISTER_SUCCESS(\"注册成功\"),\n" +
                                    "    RECOMMEND(\"推荐\"),\n" +
                                    "    QUICKLOGIN(\"快捷登录\")")
                        @RequestParam("smsType") SMSType smsType) {
        miscFeignClient.sendToMobile(mobile, smsType);
    }

    @PostMapping("register")
    @ApiOperation(value = "注册", position = 2)
    public MemberRegisterResponseVo register(@RequestBody MemberRegisterRequestVo vo){
        return memberFeignClient.register(vo);
    }

    @GetMapping("getMemberInfo")
    @ApiOperation(value = "获取会员信息,两个参数选择其中一个传递,如果都传递,则优先使用memberId, 都不传则报错", position = 3)
    public MemberVo getMemberInfo(@RequestParam(value="mobile", required = false) @ApiParam("手机号") String mobile,
                                  @RequestParam(value="memberId", defaultValue = "0", required = false) @ApiParam("会员id") Long memberId) {
        if(memberId != null && memberId != 0L)
        {
            return memberFeignClient.getUserInfoById(memberId);
        }
        if(StringUtils.isNotBlank(mobile))
        {
            return memberFeignClient.getUserInfoByMobile(mobile);
        }
        throw new IllegalArgumentException("必须传递手机号或memberId其中之一");
    }
}
